* This code replicates the computation of the geographic measure of credit market disruption in 
* Product Innovation and Credit Market Disruptions (forthcoming RFS)
* by Joao Granja and Sara Moreira
*

************************************************************************************************************************
// Step 1) Clean the CRA Small Business Lending Dataset and create a balanced panel
************************************************************************************************************************

use sbl_all, clear
destring year stcntybr, replace force
drop if year<2005

merge m:1 regulatorid respondentid year using panelTS
keep if _merge==3
drop _merge
destring rssd9001, replace force


duplicates drop rssd9001 stcntybr year , force
egen bkct = group(rssd9001 stcntybr)

tsset bkct year
tsfill, full

egen stcntybt_mode = mode(stcntybr), by(bkct)
replace stcntybr = stcntybt_mode if stcntybr==. 
drop stcntybt_mode

egen rssd9001_mode = mode(rssd9001), by(bkct)
replace rssd9001 = rssd9001_mode if rssd9001==. 
drop rssd9001_mode

egen state_mode = mode(state), by(bkct)
replace state = state_mode if state==""
drop state_mode

tsset bkct year

gen totalamount = amountloans_lower100k + amountloans_btwn100k250k + amountloans_greater250k
replace totalamount = 0 if totalamount==.

************************************************************************************************************************
// Step 2) Create the main variable of interest using the Shift-Share Approach
************************************************************************************************************************



// 2.1) Compute share of deposits
egen total_ctyr = total(totalamount),by(stcntybr year)
gen initialshare = totalamount/total_ctyr

// 2.2.) Running the regression and saving FEs

// 2.2.1 - Creating the dependent variables
gen delta_amount = (f.totalamount - totalamount)/(f.totalamount + totalamount)/2
gen delta_amount2 = (f3.totalamount - totalamount)/(f3.totalamount + totalamount)/2

// 2.2.2. - Here create Shift-Share instrument between SBL between 2007 and Year 2010

gen bartik =.
reghdfe delta_amount2 if year==2007,absorb(rssd9001 stcntybr, savefe)


replace bartik  = initialshare * __hdfe1__ if bartik==.
drop __hdfe1__
drop __hdfe2__

replace bartik = . if bartik==0
egen b= max(bartik),by(bkct)
replace bartik = b 
drop b

***********************

// 2.3 Computing the supply shock by adding-up within a county

collapse (sum) bartik totalamount, by(stcntybr year)

save bartik.dta, replace
